//
//  WYCSummaryController.m
//  51还款
//
//  Created by 中企互联 on 2017/10/30.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCSummaryController.h"
#import "tatalTableViewCell.h"
#import "WYCTotalModel.h"
#import "reslutModel.h"
#import "MQGradientProgressView.h"
#import "SJAxisView.h"
#import "SJChartLineView.h"
#import "SJLineChart.h"
@interface WYCSummaryController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *totalView;
@property(nonatomic,strong)NSMutableArray *tatalArray;
@property(nonatomic,strong)UILabel  *totalLb;
@property(nonatomic,strong)UILabel  *unAlreadyBillAmount;
@property(nonatomic,strong)UILabel  *AlreadyBillAmount;
@property(nonatomic,strong) UILabel *Repaymentlabel;
@property(nonatomic,strong)UILabel *Statement;
@property(nonatomic,strong)NSArray  *xLb;

@property (nonatomic, strong) SJLineChart *lineChart;

@end

@implementation WYCSummaryController

- (void)viewDidLoad {
    [super viewDidLoad];
    //TableViewHead界面
    
    [self setupTableView];
    //界面布局
    [self setupNav];
    
    // [self addView];
    [self setupTotalcardlist];
    self.tatalArray =[[NSMutableArray alloc]init];
    
}
- (void)addView{
    UILabel * showlabel = [[UILabel alloc] init];
    showlabel.text = @"努力开发中...";
    showlabel.textAlignment = NSTextAlignmentCenter;
    showlabel.frame = CGRectMake(0, ScreenHeight/2-30, ScreenWidth, 60* px);
    showlabel.textColor = [UIColor lightGrayColor];
    showlabel.font = [UIFont systemFontOfSize:SmallFont];
    [self.view addSubview:showlabel];
    
}

#pragma mark ------>>创建tableview
- (void)setupTableView{
    
    UIView  *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 410)];
    headview.backgroundColor = [UIColor clearColor];
    headview.userInteractionEnabled = YES;
    
    UIView  *listview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)];
    listview.backgroundColor = [UIColor whiteColor];
    listview.userInteractionEnabled = YES;
    [headview addSubview:listview];
    
    _Statement = [[UILabel alloc] init];
    _Statement.frame = CGRectMake(10,10, 200, 30);
    _Statement.text = @"本期账单（2018年1月）";
    _Statement.textAlignment = NSTextAlignmentLeft;
    _Statement.textColor = [UIColor blackColor];
    _Statement.font = [UIFont systemFontOfSize:13];
    [listview addSubview:_Statement];
    
    //信用卡
    _Repaymentlabel = [[UILabel alloc] init];
    _Repaymentlabel.frame = CGRectMake(ScreenWidth-120, 10, 100, 30);
    _Repaymentlabel.text = @"信用卡:3张";
    _Repaymentlabel.textAlignment = NSTextAlignmentRight;
    _Repaymentlabel.textColor = [UIColor blackColor];
    _Repaymentlabel.font = [UIFont systemFontOfSize:13];
    [listview addSubview:_Repaymentlabel];
    
    //总金额
    _totalLb= [[UILabel alloc] init];
    _totalLb.frame = CGRectMake(20,_Statement.frame.size.height+_Statement.frame.origin.y+15, 100, 20);
    _totalLb.textAlignment = NSTextAlignmentLeft;
    _totalLb.textColor = [UIColor blackColor];
    _totalLb.text = @"9000.00";
    _totalLb.font = [UIFont systemFontOfSize:23];
    [listview addSubview:_totalLb];
    
    
    MQGradientProgressView *progressView = [[MQGradientProgressView alloc] initWithFrame:CGRectMake(20, _totalLb.frame.size.height+_totalLb.frame.origin.y+35, ScreenWidth-40, 5)];
    progressView.colorArr = @[(id)[UIColor greenColor]];
    progressView.progress = 0.85;
    [listview addSubview:progressView];
    //已还账单
    _AlreadyBillAmount = [[UILabel alloc] init];
    _AlreadyBillAmount.frame = CGRectMake(20, progressView.frame.size.height+progressView.frame.origin.y+20, 100, 20);
    _AlreadyBillAmount.textAlignment = NSTextAlignmentCenter;
    _AlreadyBillAmount.textColor = [UIColor blackColor];
    _AlreadyBillAmount.font = [UIFont systemFontOfSize:20];
    _AlreadyBillAmount.text = @"1858.35";
    [listview addSubview:_AlreadyBillAmount];
    UILabel  *yuan = [[UILabel alloc] init];
    yuan.frame = CGRectMake(30,_AlreadyBillAmount.frame.size.height+_AlreadyBillAmount.frame.origin.y , 15, 15);
    yuan.backgroundColor = wycMain;
    yuan.layer.masksToBounds = YES;
    yuan.textAlignment = NSTextAlignmentCenter;
    yuan .layer.cornerRadius = 14;
    [listview addSubview:yuan];
    UILabel  *yuan3 = [[UILabel alloc] init];
    yuan3.frame = CGRectMake(50, _AlreadyBillAmount.frame.size.height+_AlreadyBillAmount.frame.origin.y, 80, 20);
    yuan3.textColor = [UIColor blackColor];
    yuan3.text = @"已还账单";
    yuan3.font = [UIFont systemFontOfSize:13];
    yuan3.textAlignment = NSTextAlignmentCenter;
    [listview addSubview:yuan3];
    //未还账单
    _unAlreadyBillAmount = [[UILabel alloc] init];
    _unAlreadyBillAmount.frame = CGRectMake(ScreenWidth-120,progressView.frame.size.height+progressView.frame.origin.y +20, 100, 20);
    _unAlreadyBillAmount.textAlignment = NSTextAlignmentCenter;
    _unAlreadyBillAmount.textColor = [UIColor blackColor];
    _unAlreadyBillAmount.font = [UIFont systemFontOfSize:20];
    _unAlreadyBillAmount.text = @"45666.6";
    [headview addSubview:_unAlreadyBillAmount];
    UILabel  *yuan1 = [[UILabel alloc] init];
    yuan1.frame = CGRectMake(ScreenWidth-130, _unAlreadyBillAmount.frame.size.height+_unAlreadyBillAmount.frame.origin.y , 15, 15);
    yuan1.backgroundColor = [UIColor lightGrayColor];
    yuan1.layer.masksToBounds = YES;
    yuan1.textAlignment = NSTextAlignmentCenter;
    yuan1 .layer.cornerRadius = 14;
    [listview addSubview:yuan1];
    UILabel  *yuan2 = [[UILabel alloc] init];
    yuan2.frame = CGRectMake(ScreenWidth-110, _unAlreadyBillAmount.frame.size.height+_unAlreadyBillAmount.frame.origin.y, 80, 20);
    yuan2.textColor = [UIColor blackColor];
    yuan2.text = @"未还账单";
    yuan2.font = [UIFont systemFontOfSize:13];
    yuan2.textAlignment = NSTextAlignmentCenter;
    [listview addSubview:yuan2];
    
    UIScrollView  *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,listview.frame.size.height+listview.frame.origin.y+10,ScreenWidth , 200)];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.contentSize = CGSizeMake(ScreenWidth*2, 200);  //非常重要
    scrollView.userInteractionEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [headview addSubview:scrollView];
    //横坐标数据数组
    _xLb = @[@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0",@"0"];
    
    // 初始化折线图
    self.lineChart = [[SJLineChart alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth*2-40, 200)];
    
    // 设置折线图属性
    
    self.lineChart.title = @"消费趋势图"; // 折线图名称
    self.lineChart.maxValue = 6;   // 最大值
    
    NSMutableArray *tempArr = [NSMutableArray array];
    
    NSArray *tempMonthArr = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"十二"];
    
    for (int i = 0; i<self.xLb.count; i++)
    {
        NSInteger str = [self.xLb[i] integerValue];
        NSDictionary *tempDic=@{@"item":[NSString stringWithFormat:@"%@月",tempMonthArr[i]],@"count":@(str)};
        [tempArr addObject:tempDic];
    }
    
    int maxt = [[self.xLb valueForKeyPath:@"@max.intValue"] intValue];
    
    NSMutableArray *tempYTit=[NSMutableArray array];
    for (int i=0; i<5;i++)
    {
        NSString *str = [NSString stringWithFormat:@"%d",i*maxt/6];
        [tempYTit addObject:str];
    }
    [tempYTit addObject:[NSString stringWithFormat:@"%d",maxt]];
    
    self.lineChart.maxValue =  maxt;
    self.lineChart.yMarkTitles = tempYTit;
    
    [self.lineChart setXMarkTitlesAndValues:tempArr titleKey:@"item" valueKey:@"count"];
    //设置完数据等属性后绘图折线图
    //    [self.lineChart reloadDatas];
    [self.lineChart mapping];
    
    [scrollView addSubview:self.lineChart];
    UIView *  billView = [[UIView alloc]initWithFrame:CGRectMake(0, scrollView.frame.size.height+scrollView.frame.origin.y+10, ScreenWidth, 40)];
    billView.userInteractionEnabled = YES;
    billView.backgroundColor = [UIColor whiteColor];
    [headview addSubview:billView];
    UILabel *  billLb= [[UILabel alloc] init];
    billLb.frame = CGRectMake(20,10, 100, 20);
    billLb.textAlignment = NSTextAlignmentLeft;
    billLb.textColor = [UIColor blackColor];
    billLb.text = @"账单明细";
    billLb.font = [UIFont systemFontOfSize:15];
    [billView addSubview:billLb];
    UIView  *line = [[UIView alloc]initWithFrame:CGRectMake(0, 39, ScreenWidth, 1)];
    line.backgroundColor = WYGrayColor;
    [billView  addSubview:line];
    
    self.totalView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    self.totalView.backgroundColor = WYGrayColor;
    self.totalView.dataSource = self;
    self.totalView.delegate = self;
    self.totalView.tableHeaderView = headview;
    [self.view addSubview:self.totalView];
    
    
}
#pragma make ------>>界面布局
- (void)setupNav{
    self.navigationItem.title = @"统计";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBarTintColor:wycMain];
    self.view.backgroundColor = WYCGlobalBg;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.tatalArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * strID = @"cell";
    
    tatalTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];
    
    if (cell == nil)
    {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"tatalTableViewCell" owner:self options:nil]lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WYCTotalModel * model = self.tatalArray[indexPath.row];
    
    NSString  *tye = model.type;
    cell.bankNameLb.text =model.bankName;
    cell.executeTimeLb.text =model.executeTime;
    //消费
    if ([tye isEqualToString:@"1"]) {
        cell.amountLb.text = [NSString stringWithFormat:@"消费%.2f",[model.amount doubleValue]];
        cell.amountLb.textColor = [UIColor redColor];
        cell.typeImg.image = [UIImage imageNamed:@"buy"];
        
    }
    else
    {
        cell.amountLb.text = [NSString stringWithFormat:@"还款%.2f",[model.amount doubleValue]];
        cell.amountLb.textColor = wycMain;
        cell.typeImg.image = [UIImage imageNamed:@"borro"];
        
        
    }
    return cell;
}

#pragma make ------->>>列表请求
- (void)setupTotalcardlist{
    
    NSString *cardlist = [NSString stringWithFormat:@"%@token=%@",totalcardlist,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:cardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSLog(@"统计返回结果%@",responseObject);
            NSDictionary * dict = responseObject[@"result"];
            NSString  *creditCardCount= dict[@"creditCardCount"];
            _Repaymentlabel.text = [NSString stringWithFormat:@"信用卡：%@张",creditCardCount];
            NSString  *monthBillAmount= dict[@"monthBillAmount"];
            _totalLb.text = monthBillAmount;
            //               NSArray   *everyMonthConsumeAmount = dict[@"everyMonthConsumeAmount"];
            NSString  *unAlreadyBillAmount = dict[@"unAlreadyBillAmount"];
            NSString  *alReadyBillAmount= dict[@"alReadyBillAmount"];
            NSString  *year= dict[@"year"];
            NSString  *mouth= dict[@"month"];

            _unAlreadyBillAmount.text = unAlreadyBillAmount;
            _AlreadyBillAmount.text = alReadyBillAmount;
            _Statement.text = [NSString stringWithFormat:@"本期账单（%@年%@月）",year,mouth];
            NSArray * billDetailList = dict[@"billDetailList"];
            for (NSDictionary * dic in billDetailList) {
                WYCTotalModel  * taranLationModel = [WYCTotalModel mj_objectWithKeyValues:dic];
                [self.tatalArray addObject:taranLationModel];
            }
            
        }
        
        NSLog(@"数组%@",_tatalArray);
        [self.totalView reloadData];
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    }];
    
}

@end





